home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / clipper / nfsrc21.zip / FTTEXT.C < prev    next >
C/C++ Source or Header  |  1992-10-17  |  38KB  |  1,293 lines

  1. /*
  2.  * File......: TEXT.C
  3.  * Author....: Brice de Ganahl and Steve Larsen
  4.  * CIS ID....: 76370,1532
  5.  * Date......: $Date:   17 Oct 1992 16:25:16  $
  6.  * Revision..: $Revision:   1.7  $
  7.  * Log file..: $Logfile:   C:/nanfor/src/fttext.c_v  $
  8.  * 
  9.  * This is an original work by Brice de Ganahl and Steve Larsen
  10.  * and is placed in the public domain. 
  11.  *
  12.  * Doc headers by Glenn Scott, Don Caton, and Steve Larsen
  13.  *
  14.  * Extensively revised by Steve Larsen
  15.  *
  16.  * Modification history:
  17.  * ---------------------
  18.  *
  19.  * $Log:   C:/nanfor/src/fttext.c_v  $
  20.  * 
  21.  *    Rev 1.7   17 Oct 1992 16:25:16   GLENN
  22.  * Leo cleaned up the documentation, including an errant SEEALSO
  23.  * reference.
  24.  * 
  25.  *    Rev 1.6   03 Oct 1992 02:07:38   GLENN
  26.  * Minor adjustments to file header block.
  27.  * 
  28.  *    Rev 1.5   03 Oct 1992 02:03:44   GLENN
  29.  * Major modifications by Steve Larsen, as follows:
  30.  * 
  31.  * Brice laid some wonderful groundwork with his initial release of
  32.  * these functions, however I needed more capability.  With his per-
  33.  * mission, I have made the following additions/changes to Rev. 1.4:
  34.  * 
  35.  * -  Eliminated the problem of memory for buffers being re-allocated every
  36.  *    time a file got used.
  37.  * -  Further reduced memory impact by converting from extend system memory
  38.  *    allocation techniques to virtual memory.  To accomplish this, we
  39.  *    use the Clipper v5.01 r1.29 variants of the "_v" undocumented
  40.  *    internal functions.  If these functions change in future releases, you
  41.  *    will need to locate them herein and make the appropriate changes.
  42.  * 
  43.  *    NOTE: these functions allocate and deallocate virtual memory on an
  44.  *    "as-needed" basis.  If your application makes heavy and frequent use
  45.  *    of those functions that perform a lot of buffering (ft_fInsert(),
  46.  *    ft_fDelete() and ft_fWrite()), you might consider modifying the memory
  47.  *    management scheme used herein, that is, allocate the required buffers
  48.  *    only once upon the first call to these functions, then recycle them.
  49.  * -  Added the ability to specify file open mode.
  50.  * -  Added a function to write to a record, which through a switch can either
  51.  *    over-write the current record, or insert a new one.
  52.  * -  Added functions to insert, delete and append a specified number of lines.
  53.  * -  Fixed the existing functions so that they properly handle "trailers",
  54.  *    that is, a case where the last chars in a file are not CRLF delimited.
  55.  * -  Provided checking for the possibility that the file might be terminated
  56.  *    with ^Z (1Ah), if so, ignoring it (providing consistency with non-^Z
  57.  *    terminated files).  This only occurs on the last record of a file.
  58.  * -  Eliminated a potential problem if one were to issue an ft_fUse() prior
  59.  *    actually opening any files.
  60.  * -  Replaced the original C parsing logic to determine the end-of-line (CRLF)
  61.  *    with an optimized assembler routine.  This bypassed a significant
  62.  *    performance hit.
  63.  * -  The original header (FTTEXT.h) file in now incorporated in this one file.
  64.  *    This is not necessarily an enhancement, more like laziness.
  65.  * -  Provided the (followup) author with his very first C experience!
  66.  * 
  67.  *    Steve Larsen, Dec. 7, 1991   CIS 76370,1532
  68.  * 
  69.  * -  Function changes/additions (refer to the individual doc headers for
  70.  *    details):
  71.  * 
  72.  *    FT_FSELECT( [ < nArea  > ] )                 -> nArea
  73.  *    FT_FUSE(    [ < cFile  > ][, < nMode >   ] ) -> nHandle | NIL
  74.  *    FT_FWRITELN(  < cData  >  [, < lInsert > ] ) -> NIL
  75.  *    FT_FINSERT( [ < nLines > ] )                 -> NIL
  76.  *    FT_FDELETE( [ < nLines > ] )                 -> NIL
  77.  *    FT_FAPPEND( [ < nLines > ] )                 -> NIL
  78.  * 
  79.  *    Internal Steve Larsen revisions:
  80.  * 
  81.  *     12/07/91  Original rework
  82.  *     02/13/92  Fixed _findeol(), FT_FREADLN() and FT_FGOBOT() to
  83.  *               better handle files with CRLF, LF, ^Z or nothing
  84.  *               at the EOF.  Previously, under some conditions the
  85.  *               last record was chopped by a character, depending
  86.  *               on the last character(s).
  87.  *     05/02/92  Fixed buffering and VMM allocation problem with
  88.  *               FT_FGOBOT().
  89.  *     08/26/92  Correcting problem when appending blank lines to an
  90.  *               empty file (ft_fAppend() and ft_fWriteLn()).
  91.  * 
  92.  * 
  93.  *    Rev 1.4   17 Aug 1991 15:31:08   GLENN
  94.  * Don Caton fixed some spelling errors in the doc
  95.  * 
  96.  *    Rev 1.3   15 Aug 1991 23:08:36   GLENN
  97.  * Forest Belt proofread/edited/cleaned up doc
  98.  * 
  99.  *    Rev 1.2   29 Apr 1991 08:02:12   GLENN
  100.  * Minor adjustments to documentation block
  101.  * 
  102.  *    Rev 1.1   29 Apr 1991 08:00:26   GLENN
  103.  * ft_flastrec() -- name was longer than 10 characters so linkers couldn't
  104.  * find the symbol.  Just hacked off the last "c" so it is really 
  105.  * ft_flastre().  Sorry, folks.  -- Glenn
  106.  * 
  107.  *    Rev 1.0   01 Apr 1991 01:02:48   GLENN
  108.  * Nanforum Toolkit
  109.  * 
  110.  */
  111.  
  112. /*  Notes:
  113.  
  114.      The Clipper internal functions used seem to be stable across
  115.      versions but nothing is guaranteed.  These functions begin
  116.      with _t, are used for file I/O, and are compatible with their
  117.      ANSI counterparts (just strip the _t and you have the ANSI name).
  118.      See text.h for the prototypes.
  119.  
  120.      This revision utilizes the in-line assembler feature found in MSC
  121.      6.0.  If compiling with TurboC substitute "_asm" with "asm".
  122.  
  123.      I compile these functions with the following MicroSoft C parameters:
  124.  
  125.           cl  /c /AL /Od /Zl /Zi /FPa /Gs /W3 text.c
  126.  
  127.      Note that the /Od defeats optimization and is necessary only for
  128.      compatibility with Blinker, Warplink, etc.  If you are not overlaying
  129.      this code you may want to change this to /Oalt.  Likewise, the
  130.      /Zi is for symbolic debugging info which you will want to omit in
  131.      any final compiles.
  132.  
  133.      Some sample Clipper code which would use these functions is listed
  134.      below.  It will print out the contents of this file.
  135.  
  136.               ft_fuse( "text.c" )
  137.               do while !ft_feof()
  138.                  ? ft_freadln()
  139.                  ft_fskip()
  140.               enddo
  141.               ft_fuse()
  142.  
  143.  
  144. */
  145.  
  146. /* up this number if you need more than 10 text file areas */
  147.  
  148. #define TEXT_WORKAREAS 10
  149.  
  150. #include "extend.h"
  151. #include "stdio.h"
  152. #include "share.h"
  153. #include "fcntl.h"
  154.  
  155. #define b_size     1024
  156. #define c_size     4096
  157.  
  158. #ifndef SIZE_T
  159.    #define SIZE_T
  160.    typedef unsigned int size_t;
  161. #endif
  162.  
  163. void pascal ft_fseek( void );
  164. void pascal ft_fuse( void );
  165. void pascal ft_fselect( void );
  166. void pascal ft_fgotop( void );
  167. void pascal ft_frecno( void );
  168. void pascal ft_fgobot( void );
  169. void pascal ft_fskip( void );
  170. void pascal ft_freadln( void );
  171. void pascal ft_flastre( void );
  172. void pascal ft_feof( void );
  173. void pascal ft_fgoto( void );
  174. void pascal ft_fwritel( void );
  175. void pascal ft_fdelete( void );
  176. void pascal ft_fappend( void );
  177.  
  178. int _findeol( char *buf, int buf_len );    /* in-line ASM */
  179. int _findbol( char *buf, int buf_len );    /* in-line ASM */
  180. /*void _ftwrite( char *buf, int insert );   (replaced below) */
  181. long _filewrite( long read1, long read2, long end1, long end2,
  182.                  char *c, char *c2 );
  183.  
  184. long _ft_skip( int recs );
  185.  
  186. extern int  _tclose( int );
  187. extern int  _tcreat( char*, int );
  188. extern int  _terror;
  189. extern long _tlseek( int, long, int );
  190. extern int  _topen( char*, int );
  191. extern int  _tread( int, char*, int );
  192. extern int  _twrite( int, char*, int );
  193. extern int  _tcommit( int );
  194. /* extern int  strlen( char* ); */
  195. extern int  _vAlloc( int, int );
  196. extern char *_vLock( int );
  197. extern void _vUnLock( int );
  198. extern void _vFree( int );
  199.  
  200. static long recno[TEXT_WORKAREAS];
  201. static long offset[TEXT_WORKAREAS];
  202. static int  handles[TEXT_WORKAREAS];
  203. static int  area = 0;
  204. static long last_rec[TEXT_WORKAREAS];
  205. static long last_off[TEXT_WORKAREAS];
  206. static long lastbyte[TEXT_WORKAREAS];
  207. static int  isEof[TEXT_WORKAREAS];
  208.  
  209.  
  210. /*  $DOC$
  211.  *  $FUNCNAME$
  212.  *     FT_FUSE()
  213.  *  $CATEGORY$
  214.  *     File I/O
  215.  *  $ONELINER$
  216.  *     Open or close a text file for use by the FT_F* functions
  217.